<template>
  <div class="app-container ohn">
    <div class="lb-flex lb-flex-bw">
      <span class="lh32">数据权限</span>
      <el-button
        type="primary"
        plain
        size="small"
        @click="createdDetection"
      >新增</el-button>
    </div>
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      stripe
      border
      style="width: 100%"
      class="mt8"
    >
      <el-table-column prop="objectname" label="对象名称" />
      <el-table-column prop="objectType" label="访问类型" width="" />
      <el-table-column prop="objectPath" label="对象路径" width="" />
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleDelCert(scope.row)"
          >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="fr mt20"
      :current-page="pagination.currPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pagination.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.pageTotal"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import config from "@/utils/config"
const { prefix } = config[process.env.NODE_ENV]
import { getToken } from "@/utils/auth" // get token from cookie
export default {
  data() {
    return {
      prefix: prefix,
      tableLoading: false,
      formInline: {
        name: "",
        encryptionKey: ""
      },
      tableData: [
        {
          objectname: "证书下载列表",
          objectType: "公开读写",
          objectPath: "/api/test/queryTestTradeList"
        },
        {
          objectname: "证书下载",
          objectType: "私有",
          objectPath: ""
        },
        {
          objectname: "证书上传",
          objectType: "公开读写",
          objectPath: ""
        },
        {
          objectname: "证书删除",
          objectType: "私有",
          objectPath: "/api/test/deleteTestTrade"
        }
      ],
      pagination: {
        currPage: 1,
        pageSize: 10,
        pageTotal: 0
      }
    }
  },
  created() {
    // this.handleSearchCertList();
  },
  methods: {
    handleSearchCertList() {
      this.tableLoading = true
      const fdata = {
        pageNum: this.pagination.currPage,
        pageSize: this.pagination.pageSize,
        name: this.formInline.name,
        encryptionKey: this.formInline.encryptionKey
      }
      this.$store
        .dispatch("cert/getCertList", fdata)
        .then((res) => {
          const { data, success, errorMessage } = res
          if (success) {
            this.tableData = data.list
            this.pagination.currPage = data.pageNum
            this.pagination.pageTotal = data.total
          } else {
            this.$message.error(errorMessage)
          }
        })
        .catch(() => {})
        .finally(() => {
          this.tableLoading = false
        })
    },
    handleDelCert(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$store
            .dispatch("cert/delCert", row.id)
            .then((res) => {
              const { data, success, errorMessage } = res
              if (success) {
                this.$message({
                  message: "删除成功",
                  type: "success"
                })
                this.handleSearchCertList()
              } else {
                this.$message.error(errorMessage)
              }
            })
            .catch(() => {})
            .finally(() => {})
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          })
        })
    },
    handleDownLoadCert(row) {
      this.fileName = row.name
      this.$message({
        type: "info",
        message: "文件正在下载中，请稍后~"
      })
      fetch(
        prefix.lb +
          "/api/certification/downloadByEncryptionKey?encryptionKey=" +
          row.encryptionKey,
        {
          method: "GET",
          responseType: "blob",
          headers: new Headers({
            // 自己加的头信息全都要转成string
            encryptionKey: row.encryptionKey.toString(),
            "ACCESS-TOKEN": getToken().toString()
          })
        }
      )
        .then((res) => res.blob())
        .then((data) => {
          const blobUrl = window.URL.createObjectURL(data)
          const a = document.createElement("a")
          a.download = this.fileName + ".pdf"
          a.href = blobUrl
          a.click()
          this.$message({
            type: "success",
            message: "文件下载成功"
          })
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "文件下载失败，请稍后再试~"
          })
        })
        .finally(() => {})
    },
    onSearch() {
      this.handleSearchCertList()
    },
    resetForm() {
      this.formInline.name = ""
      this.formInline.encryptionKey = ""
      this.handleSearchCertList()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.handleSearchCertList()
    },
    handleCurrentChange(val) {
      this.pagination.currPage = val
      this.handleSearchCertList()
    }
  }
}
</script>
